package cn.tedu.review;


import java.util.Objects;

/**本类用于复习Object中的常用方法*/
public class TestObject {
    public static void main(String[] args) {
        Cat c1 = new Cat();
        Cat c2 = new Cat();
        Cat c3 = new Cat("喵喵",3,99.9);
        Cat c4 = new Cat("喵喵",3,99.9);

        System.out.println(c1 == c2);//f，引用类型比较的是地址值，基本类型直接比较
        System.out.println(c3 == c4);//f
        System.out.println(c1 == c4);//f

        System.out.println(c1.hashCode());
        System.out.println(c2.hashCode());
        System.out.println(c1.equals(c2));//f
        System.out.println(c3.equals(c4));//f
        System.out.println(c1.equals(c4));//f

        System.out.println("-------------");

        System.out.println(c2);
        System.out.println(c2.toString());

        System.out.println("-------------");

        String s1 = "abc";
        char[] value = {'a','b','c'};
        String s2 = new String(value);
        String s3 = "aaa";
        System.out.println(s1 == s2);//f
        System.out.println(s1 == s3);//f

        System.out.println(s1);//abc
        System.out.println(s2);//abc


        System.out.println("---------------------");

        System.out.println(s1.equals(s2));//t,因为重写了hashCode和equals，比较的是根据属性生成的哈希码值
        System.out.println(s1.equals(s3));//f
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());

    }
}



class Cat{
    String name;
    int age;
    double price;

    public Cat() {
    }

    public Cat(String name, int age, double price) {
        this.name = name;
        this.age = age;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Cat cat = (Cat) o;
        return age == cat.age && Double.compare(cat.price, price) == 0 && Objects.equals(name, cat.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, price);
    }
}
